﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Tau.Model;
using Tau.Service;
using Tau.ViewModels;
using Tau.Web.Infrastructure;
using Product = Tau.Model.Product;

namespace Tau.Web.Areas.Admin.Controllers
{
    public class ProductAdminController : BaseAdminController
    {
        public ActionResult Index()
        {
            //Get all images to display
            var svc = new TauService();

            var products = svc.GetAllProducts();

            return View("Index", new ProductList() { Products = products });
        }

        public ActionResult AddEditProduct(int? productId)
        {
            var svc = new TauService();
            AddEditProduct addEditProductViewModel = svc.GetAddEditProductViewModel(productId);
            
            return PartialView("AddEditProduct", addEditProductViewModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddEditProduct(AddEditProduct addEditProduct)
        {
            string productDescription = Request["productDescription"];
            var svc = new TauService();

            var categoryIds = new List<int>();
            if (Request["categories[]"] != null)
            {
                string[] categoryIdStrings = Request["categories[]"].Split(',');
                foreach(string categoryId in categoryIdStrings)
                {
                    categoryIds.Add(Convert.ToInt32(categoryId));
                }
            }
            if (addEditProduct.EditingProduct.ProductId > 0)
            {
                addEditProduct.EditingProduct.Description = productDescription;
                svc.UpdateProduct(addEditProduct.EditingProduct, categoryIds);
            }
            else
            {
                Product newProduct = new Product()
                {
                    Description = productDescription,
                    Hide = addEditProduct.EditingProduct.Hide,
                    ImageId = addEditProduct.EditingProduct.ImageId,
                    Price = addEditProduct.EditingProduct.Price,
                    ProductNumber = addEditProduct.EditingProduct.ProductNumber,
                    Shipping2 = addEditProduct.EditingProduct.Shipping2,
                    ShippingCost = addEditProduct.EditingProduct.ShippingCost,
                    ShortDescription = addEditProduct.EditingProduct.ShortDescription,
                    Special = addEditProduct.EditingProduct.Special,
                    Spotlight = addEditProduct.EditingProduct.Spotlight,
                    WholesalePrice = addEditProduct.EditingProduct.WholesalePrice,
                    ProductName = addEditProduct.EditingProduct.ProductName
                };
                svc.AddProduct(newProduct, categoryIds);
            }
            
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int productId)
        {
            var svc = new TauService();
            svc.DeleteProduct(productId);
            return RedirectToAction("Index");
        }

        [CacheControl(HttpCacheability.NoCache), HttpGet]
        public ActionResult GetSortedList(string sortBy)
        {
            ProductList productList = null;

            Infrastructure.Session.SetSortBy(sortBy);
           
            var svc = new TauService();

            productList = svc.GetSortedProductList(sortBy, Infrastructure.Session.SortDescending);

            return PartialView("ProductList", productList);
        }

    }
}
